<?php

namespace app\order\model;

use think\Model;
use think\Db;
use app\common\model\Plugin;
#use app\shop\model\Shop;

class Order extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'xb_order';

    // 自动写入时间戳
    //protected $autoWriteTimestamp = true;

    // 拆分状态
    public function getOrderSplitStatus($status=99)
    {
        $statusArr = [
            1   => '已拆分',
            0   => '未拆分'
        ];
        return array_key_exists($status,$statusArr) ? $statusArr[$status] : '';
    }

    // 枚举支付状态
    public static function getPayStatus()
    {
        return [
            1   => '已支付',
            0   => '未支付'
        ];
    }

    // 订单支付状态
    public static function getPayStatusText($status=99)
    {
        $statusArr = self::getPayStatus();
        return array_key_exists($status,$statusArr) ? $statusArr[$status] : '';
    }

    // 枚举发货状态
    public static function getShippingStatus()
    {
        return [
            1   => '已发货',
            0   => '未发货'
        ];
    }

    // 订单发货状态
    public static function getShippingStatusText($status=99)
    {
        $statusArr = self::getShippingStatus();
        return array_key_exists($status,$statusArr) ? $statusArr[$status] : '';
    }

    // 枚举订单订单
    public static function getOrderStatus()
    {
        return [
            0       => '待审核',
            1       => '待支付',
            2       => '待发货',
            3       => '已打印',
            4       => '已拣货',
            5       => '已验货',
            6       => '待收件',
            7       => '待收货',
            8       => '已完成',
            9       => '取消申请',
            10      => '已取消',
            11      => '已作废',
            12      => '已删除'
        ];
    }

    // 订单状态
    public static function getOrderStatusText($status=99)
    {
        $statusArr = self::getOrderStatus();
        return array_key_exists($status,$statusArr) ? $statusArr[$status] : '';
    }

    // 订单状态文本
    public function getOrderStatsTxt($status=99)
    {
        $statusArr = $this->getOrderStatus();
        return array_key_exists($status,$statusArr) ? $statusArr[$status] : '';
    }

    // 枚举订单类型
    public static function getOrderType()
    {
        return [
            1       => '批发',
            2       => '直发',
            3       => '定制'
        ];
    }

    // 发票
    public function getInvoiceText($key=99)
    {
        $data = [
            0       => '不需要',
            1       => '需要'
        ];
        return array_key_exists($key,$data) ? $data[$key] : '';
    }

    // 订单类型文本
    public static function getOrderTypeText($key=99)
    {
        $data = self::getOrderType();
        return array_key_exists($key,$data) ? $data[$key] : '';
    }

    // 订单查询
    public function search($where=array())
    {
        return $this->where($where)->order('order_id desc')->paginate();
    }

    // 订单基本信息
    public function baseinfo($order_id)
    {
        $order_id = intval($order_id);
        
        if($order_id==0) return false;

        $data = self::get($order_id);

        return !empty($data) ? $data->getData() : array();
    }

    // 订单收货人
    public static function consigneeDelete($order_id)
    {
        $order_id = intval($order_id);

        if($order_id==0) return false;

        return Db::name('order_consignee')->where('order_id='.$order_id)->find();
    }

    // 订单详情
    public function info($order_id)
    {
        $order_id = intval($order_id);

        if($order_id==0) return false;

        $data['base'] = $this->baseinfo($order_id);
        if( empty($data['base']) ) return false;

        $data['base']['order_status_text'] = $this->getOrderStatsTxt( $data['base']['order_status'] );
        $data['base']['invoice_text'] = $this->getInvoiceText($data['base']['invoice']);

        $data['consignee'] = model('OrderConsignee')->getConsignee($order_id);

        $orders = $this->orders($order_id);
        $data['orders'] = $orders['orderData'];

        $data['goods'] = $this->orderGoods($orders['orderId']);
        //dump($data['goods']);exit;
        return $data;
    }

    // 订单拆分显示
    public function orders($order_id)
    {
        $mainOrder = $this->where("order_id=".$order_id)->find();
        $mainOrder = $mainOrder->getData();

        $orderData = [];

        $mainOrder['warehouse_name'] = Plugin::getWareHouseText($mainOrder['warehouse_id']);
        $orderData[$mainOrder['order_id']] = $mainOrder;

        if($mainOrder['is_split']==1)
        {
            $subOrder = $this->where("parent_id=".$mainOrder['order_id'])->select();
            foreach($subOrder as $o)
            {
                $o = $o->getData();
                $orderId[] = $o['order_id'];
                $shopBaseInfo = \think\Loader::model('app\shop\model\Shop')->getShopBaseInfo($o['shop_id']);
                $o['shop_name'] = isset($shopBaseInfo['name']) ? $shopBaseInfo['name'] : '';
                $orderData[$mainOrder['order_id']]['subOrders'][$o['order_id']] = $o;
            }
        }else{
            $orderId[] = $order_id;
            //$shopBaseInfo = model('Shop')->getShopBaseInfo($mainOrder['shop_id']);
            $shopBaseInfo = \think\Loader::model('app\shop\model\Shop')->getShopBaseInfo($mainOrder['shop_id']);
            $mainOrder['shop_name'] = isset($shopBaseInfo['name']) ? $shopBaseInfo['name'] : '';
            $orderData[$mainOrder['order_id']]['subOrders'][$mainOrder['order_id']] = $mainOrder;
        }
        
        return array(
            'orderId' => $orderId,
            'orderData' => $orderData
        );
    }

    // 订单商品
    public function orderGoods($orderId=array())
    {
        $orderGoods = [];

        $goods = model('OrderGoods')->getGoods( $orderId );
        $i = 0;
        foreach($goods as $g)
        {
            $orderKey = $g['order_id'];
            $classKey = $g['class_id'];
            if( isset($orderGoods[$orderKey][$classKey]) ){
                if( isset($orderGoods[$orderKey][$classKey]) ){
                    array_push($orderGoods[$orderKey][$classKey]['goods'], $g);
                }else{
                    $orderGoods[$orderKey][$classKey]['goods'][] = $g;
                }
            }else{
                $orderGoods[$orderKey][$classKey] = array('i'=>$i++, 'class_id'=>$g['class_id'], 'class_name'=>$g['class_name']);
                $orderGoods[$orderKey][$classKey]['goods'][] = $g;
            }
        }

        return $orderGoods;
    }
}